<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>类式继承</title>
</head>
<body>

<script type="text/javascript">

  // 声明父类
  const SuperClass = function () {
    let id = 1
    this.name = ['javascript']
    this.superValue = function () {
      console.log('superValue is true')
      console.log(this.id)
    }
  }

  // 为父类添加共有方法
  SuperClass.prototype.getSuperValue = function () {
    return this.superValue()
  }

  // 声明子类
  const SubClass = function () {
    // 子类实例上的自有方法
    this.subValue = function () {
      console.log('this is subValue')
    }
  }

  // 继承父类(通过原型继承)
  // 重写了原型，constructor被修改，SubClass的实例的原型中没有constructor属性
  SubClass.prototype = new SuperClass()
  // 解决丢失constructor的问题
  SubClass.prototype.constructor = SuperClass

  // 为子类添加共有方法
  SubClass.prototype.getSubValue = function () {
    return this.subValue()
  }

  var sub = new SubClass()
  var sub2 = new SubClass()

  sub.getSuperValue()
  sub.getSubValue()

  console.log(sub.id)
  console.log(sub.name)
  sub.name.push('java')
  console.log(sub2) // ['javascript', 'java']
  console.log(new SubClass())

</script>

</body>
</html>
